from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from app1.models import Emp, Dept


# Create your views here.
def index(request):
    '''主页面'''
    print('easyui index()..')
    # return HttpResponse('xxxx')
    return render(request,'main.html')

@csrf_exempt
def emp_list(request):
    if request.method=='GET':
        return render(request,'easyui/emp_list.html')
    #tree需要的数据格式：
    '''
    {"total":28,"rows":[
            {"productid":"FI-SW-01","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
            {"productid":"K9-DL-01","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
            {"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":28.50,"attr1":"Venomless","itemid":"EST-11"},
            {"productid":"RP-SN-01","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
            {"productid":"RP-LI-02","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
            {"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
            {"productid":"FL-DSH-01","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
            {"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":63.50,"attr1":"Adult Female","itemid":"EST-16"},
            {"productid":"FL-DLH-02","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
            {"productid":"AV-CB-01","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
    ]}
    '''

    q = request.POST.get('q')
    #获取分页相关信息，page：当前页，rows：每页记录数
    page_str = request.POST.get('page','1')
    if page_str.isdecimal():
        page = int(page_str)
    else:
        page = 1
    page_size_str = request.POST.get('rows','10')
    if page_size_str.isdecimal():
        page_size = int(page_size_str)
    else:
        page_size = 10
    query_dict = {}
    if q:
        query_dict['ename__contains'] = q
    #计算总页数
    queryset = Emp.objects.filter(**query_dict).order_by('-empno')
    total = queryset.count()
    p,div = divmod(total,page_size)
    if div:
        p+=1
    pages = p

    if page <1:
        page = 1
    if page > pages:
        page = pages


    #获取当前页数据
    start = (page-1)*page_size
    end = page*page_size
    page_datas = queryset[start:end]

    #响应内容
    result_dict = {}
    page_datas = [item.to_dict() for item in page_datas]
    result_dict['total'] = total
    result_dict['rows'] = page_datas

    return JsonResponse(result_dict)


@csrf_exempt
def emp_add(request):
    if request.method=='GET':
        dept_list = Dept.objects.all()
        return render(request,'easyui/emp_add.html',{'dept_list':dept_list})

    ename = request.POST.get('ename')
    sal = request.POST.get('sal')
    hiredate = request.POST.get('hiredate')
    deptno = request.POST.get('deptno')
    #print(ename,' ',sal,' ',hiredate,' ',deptno)
    #忽略校验。。。
    Emp.objects.create(ename=ename,sal=sal,hiredate=hiredate,dept_id=deptno)
    result_dict = {'status':True,}
    return JsonResponse(result_dict)

@csrf_exempt
def emp_update(request):
    if request.method=='GET':
        empno = request.GET.get('empno')
        emp = Emp.objects.filter(empno=empno).first()
        dept_list = Dept.objects.all()
        context = {'emp':emp,'dept_list':dept_list}
        return render(request,'easyui/emp_update.html',context)

    empno = request.POST.get('empno')
    ename = request.POST.get('ename')
    sal = request.POST.get('sal')
    hiredate = request.POST.get('hiredate')
    deptno = request.POST.get('deptno')
    #print(ename,' ',sal,' ',hiredate,' ',deptno)
    #忽略校验。。。
    Emp.objects.filter(empno=empno).update(ename=ename,sal=sal,hiredate=hiredate,dept_id=deptno)
    result_dict = {'status':True,}
    return JsonResponse(result_dict)

def emp_delete(request):
    empno = request.GET.get('empno')
    print('empno=',empno)
    Emp.objects.filter(empno=empno).delete()
    result_dict = {'status':True}
    return JsonResponse(result_dict)